package de.hbrs.ooka.crs.servlet;

import de.hbrs.ooka.beans.ConferenceBeanStatelessLocal;
import de.hbrs.ooka.beans.PaperBeanStatelessLocal;
import de.hbrs.ooka.beans.UserSessionStatefulLocal;
import de.hbrs.ooka.crs.servlet.constants.WebPageNames;
import de.hbrs.ooka.entities.ConferenceEntity;
import de.hbrs.ooka.entities.PaperEntity;
import de.hbrs.ooka.entities.dtos.ConferenceTransferObject;
import de.hbrs.ooka.enums.Role;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ParticipantViewServlet extends AbstractViewServlet {

    public class Triple<K, V1, V2>  {

        private K key;
        private V1 value1;
        private V2 value2;
        
        public K getKey(){
            return key;
        }
        
        public V1 getFirstValue() {
            return value1;
        }
        
        public V2 getSecondValue() {
            return value2;
        }

        public Triple(K key, V1 value1, V2 value2) {
            this.key = key;
            this.value1 = value1;
            this.value2 = value2;
        }
    }
    @EJB
    private ConferenceBeanStatelessLocal confBean;
    @EJB
    private PaperBeanStatelessLocal paperBean;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        UserSessionStatefulLocal crsBean = this.getCrsBean(request);
        String redirection = "";
        RequestDispatcher rd = null;

        if (crsBean == null || !this.isUserAuthenticated(crsBean.getUser(), Role.PARTICIPANT)) {
            request.setAttribute("message", "Not logged in");
            request.setAttribute("redirect", WebPageNames.LoginViewPage);
            rd = request.getRequestDispatcher("/" + WebPageNames.ErrorPage + ".jsp");
            if (rd != null) {
                rd.forward(request, response);
            }
        } else {
            String action = request.getParameter("action");

            request.setAttribute("name", crsBean.getUser().getName());
            request.setAttribute("roles", crsBean.getUser().getRoles());

            if (action == null) {
                List<ConferenceTransferObject> availableConfs = new ArrayList<ConferenceTransferObject>();
                List<ConferenceTransferObject> myConfs = new ArrayList<ConferenceTransferObject>();

                for (ConferenceTransferObject conferenceEntity : this.confBean.getAllConferences()) {
                    if (crsBean.getUser().getConferences().contains((ConferenceEntity) conferenceEntity)) {
                        myConfs.add(conferenceEntity);
                    } else {
                        availableConfs.add(conferenceEntity);
                    }
                }

                request.setAttribute("conferenceList", availableConfs);
                request.setAttribute("myConferenceList", myConfs);

                List<Triple<String, String, Boolean>> papers = new ArrayList<Triple<String, String, Boolean>>();
                List<PaperEntity> paps = this.paperBean.getAllPapers();
                for (PaperEntity p : paps) {
                    if (!p.getAuthor().equals(crsBean.getUser())) {
                        continue;
                    }

                    ConferenceEntity c = p.getSubmittedConference();
                    papers.add(new Triple<String, String, Boolean>(c.getName(), p.getFilename(), p.isAccepted()));
                }

                request.setAttribute("paperList", papers);

                rd = request.getRequestDispatcher("/" + WebPageNames.ParticipantViewPage + ".jsp");

                if (rd != null) {
                    rd.forward(request, response);
                }
            } else if (action.equalsIgnoreCase("addpaper")) {
                rd = request.getRequestDispatcher("/" + WebPageNames.PaperAddingPage);
                if (rd != null) {
                    rd.forward(request, response);
                }
            }
        }
    }
}
